<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv=Content-Type content="text/html; charset=utf-8">
<meta http-equiv=pragma content=no-cache>
<link href="bootstrap.min.css" rel="stylesheet">
<title>网站LNMP环境安装配置之二（CentOS 6）</title>
<style>pre {white-space: pre-wrap;white-space: -moz-pre-wrap;white-space: -pre-wrap;white-space: -o-pre-wrap;word-wrap: break-word;}</style>
</head>
<body style="margin:20px">
<h2 align=center>网站LNMP环境安装配置之二（CentOS 6）</h2>
<h3>注：网站系统为CentOS 6.9。</h3>
<p><b>一、准备工作：</b></p>
<p>1、升级系统</p>
<pre>yum update -y</pre>
<p>2、清理原有安装</p>
<pre>yum remove mysql-libs selinux* postfix http* -y</pre>
<p><b>二、测试服务器LNMP编译安装</b></p>
<p><b>1、安装编译工具</b></p>
<pre>yum install gcc cmake gcc-c++ -y</pre>
<p><b>2、安装Nginx</b></p>
<li>安装依赖</li>
<pre>yum install pcre-devel zlib-devel -y
mkdir -p /data/source
cd /data/source
wget https://www.openssl.org/source/openssl-1.0.2p.tar.gz
tar xvf openssl-1.0.2p.tar.gz -C /data/lnmp/
cd /data/lnmp/openssl-1.0.2p
./config  --prefix=/data/lnmp/lib shared --openssldir=/data/lnmp/lib/openssl
make -j5
make install
</pre>
<li>建立组和用户并设置不能ssh登录</li>
<pre>groupadd -r www
useradd -r -M -s /bin/false -g www www
sed -i "s/\/home\/www://" /etc/passwd</pre>
<li>下载</li>
<pre>cd /data/source
wget http://nginx.org/download/nginx-1.15.3.tar.gz</pre>
<li>解压</li>
<pre>tar xvf nginx-1.15.3.tar.gz</pre>
<li>编译</li>
<pre>cd nginx-1.15.3
./configure --prefix=/data/lnmp/nginx --user=www --group=www --pid-path=/run/nginx.pid --lock-path=/run/nginx.lock --http-client-body-temp-path=/tmp/client --http-proxy-temp-path=/tmp/proxy --http-fastcgi-temp-path=/tmp/fastcgi --http-uwsgi-temp-path=/tmp/uwsgi --http-scgi-temp-path=/tmp/scgi --http-log-path=/data/lnmp/log/nginx/access.log --error-log-path=/data/lnmp/log/nginx/error.log  --with-http_ssl_module --with-http_v2_module --with-stream_realip_module --with-openssl=/data/lnmp/openssl-1.0.2p --with-http_stub_status_module
make -j5
make install</pre>
<li>配置启动</li>
<pre>
https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/

sed -i "s/\/\$nginx_version//" /data/lnmp/nginx/conf/fastcgi*
sed -i "s/}/    application\/vnd.android.package-archive apk;\n}/g" /data/lnmp/nginx/conf/mime.types
mv /data/lnmp/nginx/conf/nginx.conf /data/lnmp/nginx/conf/nginx.conf.bak
mkdir /data/lnmp/nginx/conf/conf.d

cat > /data/lnmp/nginx/conf/nginx.conf << "EOF"
user  www;
worker_processes  4;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    charset  utf-8;
    sendfile        on;
    client_max_body_size 40m;
    server_tokens off;
    keepalive_timeout  65;
    gzip  on;
    include  conf.d/*.conf;
}
EOF
cat > /data/lnmp/nginx/conf/conf.d/www.conf << "EOF"
server {
    listen       80;
    server_name  localhost;
    root   /data/www;
    index  index.html index.htm index.php;

    error_page  404              /404.html;
    error_page   500 502 503 504  /50x.html;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        try_files $uri = 404;
        fastcgi_pass   127.0.0.1:9000;
        include        fastcgi.conf;
    }
}
EOF
mkdir /data/www
ln -s /data/lnmp/nginx/sbin/nginx /usr/sbin/
sed -i "s/nginx=\"\/usr\/sbin\/nginx\"/nginx=\"\/data\/lnmp\/nginx\/sbin\/nginx\"/g" /etc/init.d/nginx
sed -i "s/NGINX_CONF_FILE=\"\/etc\/nginx\/nginx.conf\"/NGINX_CONF_FILE=\"\/data\/lnmp\/nginx\/conf\/nginx.conf\"/g" /etc/init.d/nginx
</pre>
<li>打开防火墙</li>
<pre>iptables -I INPUT -p tcp --dport 80 -j ACCEPT
service iptables save
service iptables restart</pre>
<p><b>3、安装PHP</b></p>
<li>安装依赖</li>
<pre>yum install libxml2-devel gd-devel libcurl-devel -y</pre>
<li>下载</li>
<pre>cd /data/sourcewget http://cn2.php.net/distributions/php-7.2.9.tar.xz</pre>
#wget http://cn2.php.net/distributions/php-5.6.37.tar.xz
<li>解压</li>
<pre>tar xvf php-7.2.9.tar.xz</pre>
#tar xvf php-5.6.37.tar.xz
<li>编译</li>
<pre>cd php-7.2.9
#cd php-5.6.37
./configure --prefix=/data/lnmp/php --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-config-file-path=/data/lnmp/php --with-config-file-scan-dir=/data/lnmp/php/lib/php/extensions --without-sqlite3 --enable-opcache --disable-ipv6 --enable-mbstring --with-gettext --with-curl --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --without-pdo-sqlite --disable-phpdbg --with-gd --with-freetype-dir --with-png-dir --with-xpm-dir --with-jpeg-dir --with-zlib --enable-calendar --enable-exif --enable-ftp --enable-zip --enable-soap --enable-bcmath --enable-sockets --with-openssl=/data/lnmp/lib --with-mysql
#php-7.2去掉--with-mysql
make -j5
make install
#make时出现：PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.
#make install之后，运行以下命令，再次make && make install就可以了
/data/lnmp/php/bin/pear install channel://pear.php.net/PHP_Archive-0.12.0
ln -s /data/lnmp/php/bin/* /usr/bin
ln -s /data/lnmp/php/sbin/* /usr/sbin
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
#cp sapi/fpm/www.conf /data/lnmp/php/etc/php-fpm.d/
cp sapi/fpm/php-fpm.conf /data/lnmp/php/etc/
echo "zend_extension=/data/lnmp/php/lib/php/extensions/no-debug-non-zts-20170718/opcache.so" > /data/lnmp/php/lib/php/extensions/opcache.ini
#php5.6
#echo "zend_extension=/data/lnmp/php/lib/php/extensions/no-debug-non-zts-20131226/opcache.so" > /data/lnmp/php/lib/php/extensions/opcache.ini
cp php.ini-production /data/lnmp/php/php.ini
sed -i "s/;opcache.enable=0/opcache.enable=1/" /data/lnmp/php/php.ini
sed -i "s/;opcache.enable=1/opcache.enable=1/" /data/lnmp/php/php.ini
sed -i "s/;opcache.enable_cli=0/opcache.enable_cli=1/" /data/lnmp/php/php.ini
sed -i "s/;opcache.file_cache=/opcache.file_cache=\/tmp/" /data/lnmp/php/php.ini
sed -i "s/max_execution_time = 30/max_execution_time = 60/" /data/lnmp/php/php.ini
sed -i "s/upload_max_filesize = 2M/upload_max_filesize = 20M/" /data/lnmp/php/php.ini
sed -i "s/post_max_size = 8M/post_max_size = 20M/" /data/lnmp/php/php.ini
sed -i "s/display_errors = Off/display_errors = On/" /data/lnmp/php/php.ini
sed -i "s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/" /data/lnmp/php/php.ini
sed -i "s/;date.timezone =/date.timezone = Asia\/Shanghai/" /data/lnmp/php/php.ini
sed -i "s/expose_php = On/expose_php = Off/" /data/lnmp/php/php.ini
sed -i "s/;error_log = log\/php-fpm.log/error_log = \/data\/lnmp\/log\/php\/php-fpm.log/" /data/lnmp/php/etc/php-fpm.conf
sed -i "s/;slowlog = log\/\$pool.log.slow/slowlog = \/data\/lnmp\/log\/php\/slow.log/g" /data/lnmp/php/etc/php-fpm.conf
sed -i "s/;request_slowlog_timeout = 0/request_slowlog_timeout = 300/g" /data/lnmp/php/etc/php-fpm.conf
mkdir /data/lnmp/log/php
</pre>
#与php7共存时，php5的编译及配置过程
<pre>
./configure --prefix=/data/lnmp/php5 --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-config-file-path=/data/lnmp/php5 --with-config-file-scan-dir=/data/lnmp/php5/lib/php/extensions --without-sqlite3 --enable-opcache --disable-ipv6 --enable-mbstring --with-gettext --with-curl --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --without-pdo-sqlite --disable-phpdbg --with-gd --with-freetype-dir --with-png-dir --with-xpm-dir --with-jpeg-dir --with-zlib --enable-calendar --enable-exif --enable-ftp --enable-zip --enable-soap --enable-bcmath --enable-sockets --with-openssl --with-mysql

cp sapi/fpm/php-fpm.conf /data/lnmp/php5/etc/
echo "zend_extension=/data/lnmp/php5/lib/php/extensions/no-debug-non-zts-20131226/opcache.so" > /data/lnmp/php5/lib/php/extensions/opcache.ini
cp php.ini-production /data/lnmp/php5/php.ini
sed -i "s/;opcache.enable=0/opcache.enable=1/" /data/lnmp/php5/php.ini
sed -i "s/;opcache.enable_cli=0/opcache.enable_cli=1/" /data/lnmp/php5/php.ini
sed -i "s/max_execution_time = 30/max_execution_time = 60/" /data/lnmp/php5/php.ini
sed -i "s/upload_max_filesize = 2M/upload_max_filesize = 20M/" /data/lnmp/php5/php.ini
sed -i "s/post_max_size = 8M/post_max_size = 20M/" /data/lnmp/php5/php.ini
sed -i "s/display_errors = Off/display_errors = On/" /data/lnmp/php5/php.ini
sed -i "s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/" /data/lnmp/php5/php.ini
sed -i "s/;date.timezone =/date.timezone = Asia\/Shanghai/" /data/lnmp/php5/php.ini
sed -i "s/expose_php = On/expose_php = Off/" /data/lnmp/php5/php.ini
sed -i "s/;error_log = log\/php-fpm.log/error_log = \/data\/lnmp\/log\/php\/php-fpm.log/" /data/lnmp/php5/etc/php-fpm.conf
sed -i "s/;slowlog = log\/\$pool.log.slow/slowlog = \/data\/lnmp\/log\/php\/slow.log/g" /data/lnmp/php5/etc/php-fpm.conf
sed -i "s/;request_slowlog_timeout = 0/request_slowlog_timeout = 300/g" /data/lnmp/php5/etc/php-fpm.conf
mkdir /data/lnmp/log/php</pre>
<p><b>4、安装MariaDB/Mysql数据库</b></p>
<li>创建mysql用户</li>
<pre>groupadd -r mysql
useradd -r -M -s /bin/false -g mysql mysql
sed -i "s/\/home\/mysql://" /etc/passwd</pre>
<li>安装依赖</li>
<pre>
yum install ncurses-devel bison -y
</pre>
<li>下载</li>
<pre>wget http://mirrors.ustc.edu.cn/mariadb//mariadb-10.1.36/source/mariadb-10.1.36.tar.gz
#wget http://mirrors.ustc.edu.cn/mariadb//mariadb-10.3.9/source/mariadb-10.3.9.tar.gz</pre>
<li>解压</li>
<pre>tar xvf mariadb-10.1.36.tar.gz
#tar xvf mariadb-10.3.9.tar.gz</pre>
<li>编译</li>
<pre>cd mariadb-10.1.36
#cd mariadb-10.3.9
cmake . \
-DCMAKE_INSTALL_PREFIX=/data/lnmp/mariadb \
-DMYSQL_DATADIR=/data/lnmp/mariadb/data \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DDISABLE_SHARED=NO \
-DPLUGIN_MROONGA=NO \
-DPLUGIN_FEDERATED=NO \
-DPLUGIN_FEDERATEDX=NO \
-DPLUGIN_OQGRAPH=NO \
-DPLUGIN_AUTH_PAM=NO \
-DPLUGIN_AUTH_GSSAPI=NO \
-DAUTH_GSSAPI_PLUGIN_TYPE=OFF \
-DPLUGIN_SEQUENCE=NO \
-DPLUGIN_SPIDER=NO \
-DPLUGIN_FTEXAMPLE=NO \
-DPLUGIN_SPHINX=NO \
-DPLUGIN_DAEMON_EXAMPLE=NO \
-DPLUGIN_CRACKLIB_PASSWORD_CHECK=NO \
-DPLUGIN_CASSANDRA=NO \
-DSUFFIX_INSTALL_DIR= \
-DWITH_SSL=/data/lnmp/lib \
-DENABLE_DTRACE=no \
-DWITH_JEMALLOC=no \
-DPLUGIN_EXAMPLE=NO \
-DPLUGIN_EXAMPLE_KEY_MANAGEMENT=NO \
-DPLUGIN_CONNECT=NO \
-DPLUGIN_TOKUDB=NO \
-DPLUGIN_ROCKSDB=NO \
-DWITH_NUMA=OFF \
-DCONNECT_WITH_MYSQL=1 \
-DCONNECT_WITH_LIBXML2=no \
-DCONNECT_WITH_ODBC=no \
-DCONNECT_WITH_JDBC=no \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_INNODB_LZ4=OFF \
-DWITH_INNODB_LZO=OFF \
-DWITH_INNODB_SNAPPY=OFF \
-DWITH_DEBUG=no \
-DWITH_EMBEDDED_SERVER=no \
-DWITH_PROFILING=no \
-DWITH_MARIABACKUP=OFF \
-DWITH_LIBARCHIVE=OFF \
-DWITH_UNIT_TESTS=OFF \
-DWITH_UNITTEST=OFF \
-DCONC_WITH_UNITTEST=OFF \
-DINSTALL_MYSQLTESTDIR=''

make -j5
make install
ln -s /data/lnmp/mariadb/bin/* /usr/bin
chown mysql:mysql /data/lnmp/mariadb/data -R
cp support-files/my-huge.cnf /data/lnmp/mariadb/my.cnf
ln -s /data/lnmp/mariadb/my.cnf /etc
cp support-files/mysql.server /etc/init.d/mariadb
cd /data/lnmp/mariadb
scripts/mysql_install_db --user=mysql --defaults-file=/data/lnmp/mariadb/my.cnf --datadir=/data/lnmp/mariadb/data/
service mariadb start
/data/lnmp/mariadb/bin/mysqladmin -u root password 'password'
#更新或重新编译后：
rm -rf /data/lnmp/mariadb/data/test
service mariadb restart
</pre>
<p>完!</p>
</body>
</html>
